/*
 * @Author: fisher
 * @Date: 2024-03-18 17:44:12
 * @LastEditors: fisher luthor-li@qq.com
 * @LastEditTime: 2024-08-26 15:47:10
 * @FilePath: \pscluster\utils\sysInfo.js
 * @Description:
 */
const si = require('systeminformation')
const pidusage = require('pidusage')
const { execSync } = require('child_process')
exports.getSysInfo = async () => {
  const data = {}
  const dynamicData = await si.get({
    mem: 'total,used',
    graphics: 'controllers'
    // cpu: 'brand,cores',
    // osInfo: 'platform,distro,arch'
  })
  const nvdia = dynamicData.graphics.controllers.find(item =>
    item.vendor.includes('NVIDIA')
  )
  if (nvdia) {
    data.gpu = {
      total: nvdia.memoryTotal,
      used: nvdia.memoryUsed
    }
  } else {
    data.gpu = undefined
  }
  data.memory = dynamicData.mem
  return data
}

exports.getProcess = async pid => {
  // 使用 wmic 获取特定父进程ID的所有子进程的PID
  let output = execSync(
    `wmic process where (ParentProcessId=${pid}) get ProcessId`
  ).toString()

  // 解析 wmic 输出结果
  let processIds = output
    .split('\n')
    .slice(1)
    .filter(line => line.trim() !== '') // 剔除空行和标题行

  // 输出 ProcessId 信息

  const usage = await pidusage(processIds[0].trim())
  return usage
}
